package com.example.administrator.readbook.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.adapter.AdapterSearchBook;
import com.example.administrator.readbook.bean.SearchBookBean;
import com.example.administrator.readbook.http.ServiceFactory;
import com.example.administrator.readbook.http.ServiceUrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.example.administrator.readbook.http.ServiceFactory.schedulersTransformer;


public class SearchActivity extends AppCompatActivity {
    private EditText searchEt;
    private TextView searchTv;
    private ListView searchListview;
    private AdapterSearchBook adapterSearchBook;
    private List<SearchBookBean.BooksBean> searchBookList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchEt = findViewById(R.id.search_text);
        searchTv = findViewById(R.id.search_book);
        searchListview = findViewById(R.id.search_list);
        adapterSearchBook = new AdapterSearchBook(this, searchBookList);
        searchListview.setAdapter(adapterSearchBook);
        searchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSearchData();
            }
        });
        searchListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchActivity.this, BookSourceActivity.class);
                intent.putExtra("bookId", searchBookList.get(position).get_id());
                intent.putExtra("bookName", searchBookList.get(position).getTitle());
                startActivity(intent);
            }
        });
    }

    private void getSearchData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("query", searchEt.getText().toString());
        params.put("start", "0");
        params.put("limit", "5");
        ServiceUrl searchService = ServiceFactory.getServiceBook();
        searchService.searchBookData(params).compose(schedulersTransformer).subscribe(new Observer<SearchBookBean>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(SearchBookBean searchBookBean) {
                if (searchBookBean != null) {
                    searchBookList.clear();
                    searchBookList.addAll(searchBookBean.getBooks());
                    adapterSearchBook.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }
}
